<?php
require_once "SelfCheckCase.php";
require_once dirname(__FILE__) . "/../../../util/site/require.php";
class SDOAppsSelfCheck extends SelfCheckCase
{
    
    public static function caseDB()
    {
        #自检错误
        $errs = array();
        $stat = 0;
        $passed = array();
        $db_configs = Oa_Util_Config::getConfig('db_physical');
        foreach ($db_configs as $mapid => $db_config) {
            if (!is_array($db_config)) {
                continue;
            }
            
            if (isset($db_config['read'])) {
                if (isset($db_config['db_user'])) {
                    $dbuser = $db_config['db_user'];
                    $dbpwd  = $db_config['db_pwd'];
                } else {
                    $dbuser = $db_configs['db_user'];
                    $dbpwd  = $db_configs['db_pwd'];
                }
                
                $dbhost = $db_config['read']['host'];
                $link = @mysql_connect($dbhost, $dbuser, $dbpwd);
                if (!$link) {
                    $stat++;
                    $errs[] = "Map:{$mapid},Mysql({$dbuser}@{$dbhost}) connect failed";
                } else {
                    @mysql_close($link);
                }
                
                $dbhost = $db_config['write']['host'];
                $link = @mysql_connect($dbhost, $dbuser, $dbpwd);
                if (!$link) {
                    $stat++;
                    $errs[] = "Map:{$mapid},Mysql({$dbuser}@{$dbhost}) connect failed";
                } else {
                    @mysql_close($link);
                }
            } elseif (isset($db_config['host'])) {
                $dbhost = $db_config['host'];
                if (isset($db_config['db_user'])) {
                    $dbuser = $db_config['db_user'];
                    $dbpwd  = $db_config['db_pwd'];
                } else {
                    $dbuser = $db_configs['db_user'];
                    $dbpwd  = $db_configs['db_pwd'];
                }
                $link = @mysql_connect($dbhost, $dbuser, $dbpwd);
                if (!$link) {
                    $stat++;
                    $errs[] = "Map:{$mapid},Mysql({$dbuser}@{$dbhost}) connect failed";
                } else {
                    @mysql_close($link);
                }
            }
        }
        
        $msg = "";
        if (empty($errs))
        {
            $msg = "ok";
        }
        else
        {
            $msg = implode('|', $errs);
        }
        
        return self::reportError($stat, $msg);
    }
    
    public static function caseMEMCACHE()
    {
        #Memcache 检测
        $errs = array();
        $stat = 0;
        $memcache_configs = Oa_Util_Config::getConfig('cache_physical');
        $memcache = new Memcache;
        foreach ($memcache_configs as $memcache_config) 
        {
            $host = $memcache_config['host'];
            $port = $memcache_config['port']; 
            if (!@$memcache->connect($host, $port)) {
                $stat++;
                $errs[] = "Memcache({$host}:{$port}) connect failed!";
            }
        }
        
        $msg = "";
        if (empty($errs))
        {
            $msg = "ok";
        }
        else
        {
            $msg = implode('|', $errs);
        }
        return self::reportError($stat, $msg);        
    }
}